package Servlets;
import Clases.Persona;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Kenneth
 */
public class ManejadorTareas extends HttpServlet {
  
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        ServletConfig config=getServletConfig();
        HttpSession mySession=request.getSession();
        Persona user=(Persona)mySession.getAttribute("user");
        String fecha=request.getParameter("fecha");
        try {
            String action=request.getParameter("action");
            if(action.compareTo("obtenerTareas")==0){
                String xml= user.obtenerTareaUser(fecha);
                response.setContentType("text/xml;charset=UTF-8");
                out.println(xml);


            }
            if(action.compareTo("consultarTodas")==0){

                String xml=user.obtenerTareas();
                System.out.println(xml);
                response.setContentType("text/xml;charset=UTF-8");
                out.println(xml);


            }/*
            if(action.compareTo("ingreso")==0){
                try {
                    response.setContentType("text/html;charset=UTF-8");
                    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
                    Session session = sessionFactory.openSession();
                    Transaction tx = session.beginTransaction();
                    String nombre = request.getParameter("nombreTarea");
                    String descripcion = request.getParameter("descripcionTarea");
                    String estado = request.getParameter("estado");
                    String importancia = request.getParameter("importancia");
                    String tipo = request.getParameter("tipo");
                    String idLugar = "001";
                    Tarea theEvent = new Tarea();
                    theEvent.setNombre(nombre);
                    theEvent.setDescripcion(descripcion);
                    theEvent.setEstado(estado);
                    theEvent.setImportancia(importancia);
                    theEvent.setTipo(tipo);
                    theEvent.setIdlugar(idLugar);
                    session.save(theEvent);
                    tx.commit();
                    session.close();
        } catch (HibernateException e) {
            e.printStackTrace();
        }*/
        } catch(Exception e){System.out.println(e.getMessage());}
        finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
